using Mathmall.Administration.Data;
using Mathmall.Administration.Models;
using Microsoft.EntityFrameworkCore;

namespace Mathmall.Administration.Mapper;

public class MenuMapper : BaseMapper
{
    public MenuMapper(AdministrationContext context) : base(context)
    {
    }

    /// <summary>
    /// 添加菜单
    /// </summary>
    /// <param name="menu"></param>
    /// <returns></returns>
    public async Task<int> CreateMenu(Menu menu)
    {
        _context.Menus.Add(menu);
        return await _context.SaveChangesAsync();
    }

    /// <summary>
    /// 获取全部菜单数据
    /// </summary>
    /// <returns></returns>
    public async Task<List<Menu>> GetMenus()
    {
        return await _context.Menus.AsQueryable().ToListAsync();
    }

    /// <summary>
    /// 更新菜单
    /// </summary>
    /// <param name="menuToUpdate"></param>
    /// <returns></returns>
    public async Task<int> UpdateMenu(Menu newMenu)
    {
        return await _context.Menus.Where(menu => newMenu.MenuID == menu.MenuID)
            .ExecuteUpdateAsync(setters =>
                setters.SetProperty(menu => menu.MenuName, newMenu.MenuName)
                    .SetProperty(menu => menu.AuthorityValue, newMenu.AuthorityValue));
    }

    /// <summary>
    /// 删除菜单
    /// </summary>
    /// <param name="menuID"></param>
    /// <returns></returns>
    public async Task<int> DeleteMenu(int menuID)
    {
        return await _context.Menus.Where(menu => menu.MenuID == menuID || menu.ParentId == menuID)
            .ExecuteDeleteAsync();
    }
}